use buddy_system_allocator::LockedHeap;
use crate::config::KERNEL_HEAP_SIZE;

#[global_allocator]
static HEAP_ALLOCATOR: LockedHeap = LockedHeap::empty();

static mut HEAP_SPACE: [u8; KERNEL_HEAP_SIZE] = [0; KERNEL_HEAP_SIZE];

#[alloc_error_handler]
pub fn handle_alloc_error(layout: core::alloc::Layout) -> ! {
    panic!("Heap allocation error, layput = {:?}", layout);
}

pub fn init_heap() {
    unsafe {
        HEAP_ALLOCATOR
            .lock()
            .init(
            HEAP_SPACE.as_ptr() as usize,
             KERNEL_HEAP_SIZE
            );
    }
}

#[allow(unused)]
pub fn heap_test() {
    use alloc::boxed::Box;
    use alloc::vec::Vec;
    use alloc::collections::binary_heap::BinaryHeap;

    #[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
    struct A {
        val: Option<usize>,
        f: i32,
        i: isize,
        u: usize,
    }

    extern "C" {
        fn sbss();
        fn ebss();
    }

    let bss_range = sbss as usize..ebss as usize;
    let a = Box::new(5);
    assert_eq!(*a, 5);
    assert!(bss_range.contains(&(a.as_ref() as *const _ as usize)));
    drop(a);

    let mut v: Vec<usize> = Vec::new();
    for i in 0..500 {
        v.push(i);
    }

    for i in 0..500 {
        assert_eq!(v[i], i);
    }

    assert!(bss_range.contains(&(v.as_ptr() as usize)));
    drop(v);

    let mut v: Vec<A> = Vec::new();

    for i in 0..500 {
        let a = A {
            val: Some(3),
            f: 1,
            i: 0,
            u: 2
        };
        v.push(a);
    }
    let a = A {
        val: Some(3),
        f: 1,
        i: 0,
        u: 2
    };
    for i in 0..500 {
        assert_eq!(v[i], a);
    }
    drop(v);
    info!("heap_test passed!");
}